﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Light.Utility
{
    /// <summary>
    /// Winform移动控件
    /// </summary>
    public class WinHelper
    {
        /// <summary>
        /// 要移动的控件
        /// </summary>
        public Control TargetControl { get; set; }

        /// <summary>
        /// 键盘移动操作时的单位
        /// </summary>
        public int Unit { get; set; }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public WinHelper()
        {
            Unit = 1;
        }

        /// <summary>
        /// 要移动的控件
        /// </summary>
        /// <param name="ctl"></param>
        public WinHelper(Control ctl)
            : this()
        {
            RegistEvent(ctl);
        }

        /// <summary>
        /// 注册事件，移动准备
        /// </summary>
        /// <param name="ctl">当前操纵控件(目标控件)</param>
        public void RegistEvent(Control ctl)
        {
            TargetControl = ctl;
            ctl.MouseDown += new MouseEventHandler(ctl_MouseDown);
            ctl.MouseMove += new MouseEventHandler(ctl_MouseMove);
            ctl.MouseUp += new MouseEventHandler(ctl_MouseUp);
            ctl.PreviewKeyDown += new PreviewKeyDownEventHandler(ctl_PreviewKeyDown);
        }


        /// <summary>
        /// 注册事件，移动准备
        /// </summary>
        /// <param name="ctl">当前操纵控件</param>
        /// <param name="targetCtl">要移动的控件</param>
        public void RegistEvent(Control ctl, Control targetCtl)
        {
            RegistEvent(ctl);
            TargetControl = targetCtl;
        }

        void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (!beginMove) return;

            int x_distance = e.X - mouse_StartPoint.X;
            int y_distance = e.Y - mouse_StartPoint.Y;

            TargetControl.Location = new Point(TargetControl.Location.X + x_distance, TargetControl.Location.Y + y_distance);
        }


        bool beginMove = false;
        Point mouse_StartPoint = new Point();



        void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            beginMove = true;
            mouse_StartPoint = new Point(e.X, e.Y);
        }

        void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            beginMove = false;
        }

        void ctl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.Alt) //调整大小
            {
                switch (e.KeyCode)
                {
                    case Keys.Left:
                        TargetControl.Width -= Unit;
                        break;
                    case Keys.Right:
                        TargetControl.Width += Unit;
                        break;
                    case Keys.Down:
                        TargetControl.Height += Unit;
                        break;
                    case Keys.Up:
                        TargetControl.Height -= Unit;
                        break;
                }
                return;
            }
            if (e.Control)
            {
                switch (e.KeyCode) //调整位置
                {
                    case Keys.Left:
                        TargetControl.Left -= Unit;
                        break;
                    case Keys.Right:
                        TargetControl.Left += Unit;
                        break;
                    case Keys.Up:
                        TargetControl.Top -= Unit;
                        break;
                    case Keys.Down:
                        TargetControl.Top += Unit;
                        break;
                }
            }
        }
    }
}
